package com.moo.order.controller;

import com.moo.order.model.Order;
import com.moo.order.service.OrderService;
import com.moo.order.vo.OrderVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Project Name: moo-order
 * Author: fhw
 * File Name: OrderController
 * Package Name: com.moo.order.controller
 * Date: 2020/7/25 9:49 下午
 * Copyright (c) 2020,All Rights Reserved.
 */
@Slf4j
@RestController
@RequestMapping("order")
@Api(value = "OrderController")
public class OrderController {

    @Autowired
    OrderService orderService;

    @ApiOperation(value = "订单列表")
    @GetMapping(value = "list")
    public ResponseEntity list() {
        try {
            List<Order> orderList = orderService.listOrders();
            List<OrderVo> memberVoList = new Order().toOrderVoList(orderList);
            return new ResponseEntity<>(memberVoList, HttpStatus.OK);
        } catch (Exception e) {
            log.error("获取订单列表发生错误 {} ", e.getMessage());
            return new ResponseEntity<>("获取订单列表发生错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

    @ApiOperation(value = "订单信息")
    @GetMapping(value = "/{id}")
    public ResponseEntity info(@PathVariable("id") Long id) {
        try {
            Order order = orderService.findById(id);
            if (order == null) {
                log.error("获取订单信息异常 {} ", id);
                return new ResponseEntity<>("获取订单信息异常 {} " + id, HttpStatus.INTERNAL_SERVER_ERROR);
            }
            OrderVo orderVo = order.toOrderVo();
            return new ResponseEntity<>(orderVo, HttpStatus.OK);
        } catch (Exception e) {
            log.error("获取订单信息发生错误 {} ", e.getMessage());
            return new ResponseEntity<>("获取订单信息发生错误", HttpStatus.INTERNAL_SERVER_ERROR);
        }
    }

}
